package com.info.zhiduoduo.repository.mapper.foreign;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.info.zhiduoduo.repository.dto.foreign.ForeignProjectInvoiceListDto;
import com.info.zhiduoduo.repository.dto.foreign.MergeInvoiceListDto;
import com.info.zhiduoduo.repository.entity.foreign.ForeignInvoice;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

public interface ForeignInvoiceMapper extends BaseMapper<ForeignInvoice> {

    /**
     * 第三方项目发票列表
     * @param name
     * @param startDate
     * @param endDate
     * @param state
     * @param sort
     * @return
     */
    @Select(" select invoice.id,invoice.amount,invoice.project_id,invoice.createtime,invoice.state,invoice.merge_id,invoice.whether_merge, " +
            " ca.realname,project.project_name,invoice.project_service_name,project.createtime as projectCreateTime, " +
            " ftr.pay_account,comp.compname " +
            " FROM zdd_foreign_invoice invoice " +
            " LEFT JOIN zdd_foreign_project project on project.project_id = invoice.project_id " +
            " LEFT JOIN zdd_crowdsource_account ca on ca.crowdsourceid = invoice.crowdsourceid " +
            " LEFT JOIN zdd_foreign_trade_record ftr on ftr.project_id = invoice.project_id " +
            " LEFT JOIN zdd_company comp on comp.compid = project.compid " +
            " where concat(ca.realname,invoice.project_service_name,comp.compname) like '%${name}%' " +
            " AND if(#{state} != 0, invoice.state = #{state}, 1 = 1) " +
            " AND IF(#{moneyCondition} = 1, invoice.amount <= 500, 1 = 1) " +
            " AND IF(#{moneyCondition} = 2, invoice.amount > 500, 1 = 1) " +
            " AND IF(ISNULL(#{startAmount}) OR #{startAmount} = '',1 = 1 , invoice.amount >=  #{startAmount} ) " +
            " AND IF(ISNULL(#{endAmount}) OR #{endAmount} = '',1 = 1 , invoice.amount <=  #{endAmount} ) " +
            " AND project.createtime between #{startDate} and #{endDate} " +
            " ${sort}")
    List<ForeignProjectInvoiceListDto> foreignInvoice(String name, Date startDate, Date endDate, int state, String sort, int moneyCondition,String startAmount,String endAmount);

    /**
     * 第三方项目发票列表-合并开票列表
     * @param name
     * @param startDate
     * @param endDate
     * @param state
     * @return
     */
    @Select(" select id,amount,state,entrusted_operator,project_service_name,name_of_party_b, " +
            " createtime,whether_merge,type,merge_remarks,enclosure_info " +
            " FROM zdd_foreign_invoice " +
            " where name_of_party_b like '%${name}%' " +
            " AND if(#{state} != 0, state = #{state}, 1 = 1)" +
            " AND createtime between #{startDate} and #{endDate} " +
            " AND whether_merge = 1")
    List<MergeInvoiceListDto> mergeInvoiceList(String name, Date startDate, Date endDate, int state);

    /**
     * 查看关联的所有单条发票信息
     * @param id
     * @return
     */
    @Select(" select invoice.id,invoice.amount,invoice.project_id,invoice.createtime,invoice.state,invoice.merge_id,invoice.whether_merge, " +
            " ca.realname,ca.idno,project.project_name,invoice.project_service_name,c.phone " +
            " FROM zdd_foreign_invoice invoice " +
            " LEFT JOIN zdd_foreign_project project on project.project_id = invoice.project_id " +
            " LEFT JOIN zdd_crowdsource_account ca on ca.crowdsourceid = invoice.crowdsourceid " +
            " LEFT JOIN zdd_crowdsource c on c.crowdsourceid = invoice.crowdsourceid " +
            " where merge_id = #{id} " )
    List<ForeignProjectInvoiceListDto> queryAllRelationInvoice(@Param("id") int id);

    /**
     * 查询出所有未开票并且未压缩过自然人身份证文件的合并开票
     * @return
     */
    @Select(" SELECT * FROM zdd_foreign_invoice WHERE state = 2 AND enclosure_info IS NULL ")
    List<ForeignInvoice> queryNoCompressedInvoice();
}
